package com.ruoyi.mall.sms.service.impl;

import com.ruoyi.mall.sms.domain.SmsCoupon;
import com.ruoyi.mall.sms.domain.SmsCouponProductCategoryRelation;
import com.ruoyi.mall.sms.domain.SmsCouponProductRelation;
import com.ruoyi.mall.sms.domain.dto.SmsCouponDto;
import com.ruoyi.mall.sms.mapper.SmsCouponMapper;
import com.ruoyi.mall.sms.mapper.SmsCouponProductCategoryRelationMapper;
import com.ruoyi.mall.sms.mapper.SmsCouponProductRelationMapper;
import com.ruoyi.mall.sms.service.ISmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优惠券Service业务层处理
 *
 * @author xuxing
 * @date 2021-02-12
 */
@Service
public class SmsCouponServiceImpl implements ISmsCouponService
{
    @Autowired
    private SmsCouponMapper smsCouponMapper;

    @Autowired
    private SmsCouponProductCategoryRelationMapper smsCouponProductCategoryRelationMapper;

    @Autowired
    private SmsCouponProductRelationMapper smsCouponProductRelationMapper;

    /**
     * 查询优惠券
     *
     * @param id 优惠券ID
     * @return 优惠券
     */
    @Override
    public SmsCouponDto selectSmsCouponById(Long id)
    {
        SmsCoupon smsCoupon = smsCouponMapper.selectSmsCouponById(id);
        SmsCouponDto smsCouponDto = new SmsCouponDto();
        smsCouponDto.setSmsCoupon(smsCoupon);
        if (smsCoupon.getUseType() == 1) {
            SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
            smsCouponProductCategoryRelation.setCouponId(id);
            List<SmsCouponProductCategoryRelation> couponProductCategoryRelationList = smsCouponProductCategoryRelationMapper.selectSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelation);
            smsCouponDto.setCouponProductCategoryRelationList(couponProductCategoryRelationList);
        } else if (smsCoupon.getUseType() == 2) {
            SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
            smsCouponProductRelation.setCouponId(id);
            List<SmsCouponProductRelation> couponProductRelationList = smsCouponProductRelationMapper.selectSmsCouponProductRelationList(smsCouponProductRelation);
            smsCouponDto.setCouponProductRelationList(couponProductRelationList);
        }
        return smsCouponDto;
    }

    /**
     * 查询优惠券列表
     *
     * @param smsCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<SmsCoupon> selectSmsCouponList(SmsCoupon smsCoupon)
    {
        return smsCouponMapper.selectSmsCouponList(smsCoupon);
    }

    /**
     * 新增优惠券
     *
     * @param smsCouponDto 优惠券
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSmsCoupon(SmsCouponDto smsCouponDto)
    {
        SmsCoupon smsCoupon = smsCouponDto.getSmsCoupon();
        int rows = smsCouponMapper.insertSmsCoupon(smsCoupon);
        Long couponId = smsCoupon.getId();
        if (smsCoupon.getUseType() == 1) {
            List<SmsCouponProductCategoryRelation> couponProductCategoryRelationList = smsCouponDto.getCouponProductCategoryRelationList();
            couponProductCategoryRelationList.forEach(item -> {
                item.setCouponId(couponId);
                smsCouponProductCategoryRelationMapper.insertSmsCouponProductCategoryRelation(item);
            });
        } else if (smsCoupon.getUseType() == 2) {
            List<SmsCouponProductRelation> couponProductRelationList = smsCouponDto.getCouponProductRelationList();
            couponProductRelationList.forEach(item -> {
                item.setCouponId(couponId);
                smsCouponProductRelationMapper.insertSmsCouponProductRelation(item);
            });
        }
        return rows;
    }

    /**
     * 修改优惠券
     *
     * @param smsCouponDto 优惠券
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSmsCoupon(SmsCouponDto smsCouponDto)
    {
        SmsCoupon smsCoupon = smsCouponDto.getSmsCoupon();
        Long couponId = smsCoupon.getId();
        SmsCoupon coupon = smsCouponMapper.selectSmsCouponById(couponId);
        if (coupon.getUseType() == 1) {
            SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
            smsCouponProductCategoryRelation.setCouponId(couponId);
            Long[] ids = smsCouponProductCategoryRelationMapper.selectSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelation).stream().map(SmsCouponProductCategoryRelation::getId).toArray(Long[]::new);
            smsCouponProductCategoryRelationMapper.deleteSmsCouponProductCategoryRelationByIds(ids);
        } else if (coupon.getUseType() == 2) {
            SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
            smsCouponProductRelation.setCouponId(couponId);
            Long[] ids = smsCouponProductRelationMapper.selectSmsCouponProductRelationList(smsCouponProductRelation).stream().map(SmsCouponProductRelation::getId).toArray(Long[]::new);
            smsCouponProductRelationMapper.deleteSmsCouponProductRelationByIds(ids);
        }
        if (smsCoupon.getUseType() == 1) {
            List<SmsCouponProductCategoryRelation> couponProductCategoryRelationList = smsCouponDto.getCouponProductCategoryRelationList();
            couponProductCategoryRelationList.forEach(item -> {
                item.setCouponId(couponId);
                smsCouponProductCategoryRelationMapper.insertSmsCouponProductCategoryRelation(item);
            });
        } else if (smsCoupon.getUseType() == 2) {
            List<SmsCouponProductRelation> couponProductRelationList = smsCouponDto.getCouponProductRelationList();
            couponProductRelationList.forEach(item -> {
                item.setCouponId(couponId);
                smsCouponProductRelationMapper.insertSmsCouponProductRelation(item);
            });
        }
        return smsCouponMapper.updateSmsCoupon(smsCoupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSmsCouponByIds(Long[] ids) {
        for (Long id : ids) {
            SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
            smsCouponProductCategoryRelation.setCouponId(id);
            Long[] cIds = smsCouponProductCategoryRelationMapper.selectSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelation).stream().map(SmsCouponProductCategoryRelation::getId).toArray(Long[]::new);
            if (cIds.length > 0) {
                smsCouponProductCategoryRelationMapper.deleteSmsCouponProductCategoryRelationByIds(cIds);
            }
            SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
            smsCouponProductRelation.setCouponId(id);
            Long[] pIds = smsCouponProductRelationMapper.selectSmsCouponProductRelationList(smsCouponProductRelation).stream().map(SmsCouponProductRelation::getId).toArray(Long[]::new);
            if (pIds.length > 0) {
                smsCouponProductRelationMapper.deleteSmsCouponProductRelationByIds(pIds);
            }
        }
        return smsCouponMapper.deleteSmsCouponByIds(ids);
    }

    /**
     * 删除优惠券信息
     *
     * @param id 优惠券ID
     * @return 结果
     */
    @Override
    public int deleteSmsCouponById(Long id)
    {
        SmsCouponProductCategoryRelation smsCouponProductCategoryRelation = new SmsCouponProductCategoryRelation();
        smsCouponProductCategoryRelation.setCouponId(id);
        Long[] cIds = smsCouponProductCategoryRelationMapper.selectSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelation).stream().map(SmsCouponProductCategoryRelation::getId).toArray(Long[]::new);
        if (cIds.length > 0) {
            smsCouponProductCategoryRelationMapper.deleteSmsCouponProductCategoryRelationByIds(cIds);
        }
        SmsCouponProductRelation smsCouponProductRelation = new SmsCouponProductRelation();
        smsCouponProductRelation.setCouponId(id);
        Long[] pIds = smsCouponProductRelationMapper.selectSmsCouponProductRelationList(smsCouponProductRelation).stream().map(SmsCouponProductRelation::getId).toArray(Long[]::new);
        if (pIds.length > 0) {
            smsCouponProductRelationMapper.deleteSmsCouponProductRelationByIds(pIds);
        }
        return smsCouponMapper.deleteSmsCouponById(id);
    }
}
